package com.youzheng.controller;

import com.youzheng.entity.Product;
import com.youzheng.entity.Type;
import com.youzheng.service.IProductService;
import com.youzheng.service.ITypeService;
import com.github.pagehelper.PageInfo;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {
    @Resource
    private IProductService productService;
    @Resource
    private ITypeService typeService;
    @RequestMapping("/list.do")
    public String list(ModelMap map,String pname,@RequestParam(defaultValue = "1")Integer pageNum,@RequestParam(defaultValue = "2")Integer pageSize){


        PageInfo<Product> products=productService.findAllProducts(pageNum,pageSize,pname);
        map.addAttribute("products",products);
        map.addAttribute("pname",pname);

        BasicConfigurator.configure();
        Logger logger = Logger.getLogger(ProductController.class);




        return "product_list";
    }
    @RequestMapping("/preUpdate.do")
    public String preUpdata(Integer pid,ModelMap map){
        Product product =productService.findProductByPid(pid);
        map.addAttribute("product",product);
        List<Type> typeList=typeService.findAllTypes();
        map.addAttribute("typeList",typeList);
        return "product_update";
    }
    @RequestMapping("/update.do")
    public String update(Product product){
        productService.updateProduct(product);
        return "rediect:/product/list.do";
    }
    @RequestMapping("/delete.do")
    public String delete(Integer id){
        productService.deleteProduct(id);
        return "success";
    }
}
